﻿using System;
using System.Collections.Generic;

namespace Pyramid.Hardware
{
    public sealed class FixedInputLine
        : ILine
    {
        private static readonly FixedInputLine @true = new FixedInputLine(Trivalent.True);

        private static readonly FixedInputLine unknown = new FixedInputLine(Trivalent.Unknown);

        private static readonly FixedInputLine @false = new FixedInputLine(Trivalent.False);

        private Trivalent _signal;

        public FixedInputLine(Trivalent signal)
        {
            _signal = signal;
        }

        #region ILine Members

        public Trivalent Signal
        {
            get
            {
                return _signal;
            }
        }

        event EventHandler<SignalChangeEventArgs> ILine.SignalChange
        {
            add { }
            remove { }
        }

        #endregion

        public static FixedInputLine True
        {
            get
            {
                return @true;
            }
        }

        public static FixedInputLine Unknown
        {
            get
            {
                return unknown;
            }
        }

        public static FixedInputLine False
        {
            get
            {
                return @false;
            }
        }

        public static IEnumerable<FixedInputLine> CreateBus(ITernary value)
        {
            return CreateBus(value.ToInt27(), value.TritSize);
        }

        public static IEnumerable<FixedInputLine> CreateBus(Int27 value, int busSize)
        {
            var tritValues = value.ToTritArray();

            var result = new FixedInputLine[busSize];

            for (int lineIndex = 0; lineIndex < busSize; lineIndex++)
            {
                if (lineIndex < tritValues.Length)
                {
                    result[lineIndex] = new FixedInputLine(tritValues[lineIndex]);
                }
                else
                {
                    result[lineIndex] = FixedInputLine.Unknown;
                }
            }

            return result;
        }
    }
}
